package com.itheima.controller;

import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.UploadUtils;
import com.itheima.health.vo.SetmealVO;
import io.swagger.annotations.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @program: health_paret
 * @description:
 * @author: Mr.Wang
 * @create: 2020-11-07 21:57
 * 随便改改实验哈哈哈
 **/
@RestController
@Api(tags = "套餐管理开发")
public class SetmealWeb {
    @Reference
    private SetmealService setmealService;
    @Reference
    private CheckGroupService checkGroupService;
    //@Autowired
    //private RedisTemplate redisTemplate;

    //图片上传
    @PostMapping("setmeal/upload")
    public Result upload(@RequestParam("imgFile")MultipartFile multipartFile){
        String s = null;
        try {
            String originalFilename = multipartFile.getOriginalFilename();
            //获取随机文件名字
           s = UploadUtils.generateRandonFileName(originalFilename);
            AliyunUtils.uploadMultiPartFileToAliyun(multipartFile.getBytes(),s);
            RedisUtil.addToSet(RedisConstant.ALL_SETMEAL_PIC_SET,s);
            RedisUtil.set(RedisConstant.SINGLE_PIC+s,s,30,TimeUnit.SECONDS);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(s);
    }
    //@Reference
    //private CheckGroupService checkGroupService;
    ////编辑中根据基本信息的id进行回滚检查项目、
    @GetMapping("setmeal/findCheckgroupInfoSetmeal/{id}")
    public Result findCheckItemInfoByGroupId(@PathVariable int id){
        SetmealVO checkgroupInfoBySetmealId = setmealService.findCheckgroupInfoBySetmealId(id);

        return new Result(true,"输入错误",checkgroupInfoBySetmealId);
    }


   // 查询所有检查组的信息
    @GetMapping("setmeal/findall")
    public Result findAll() {
        List<CheckGroup> list = checkGroupService.list();
        return new Result(true, "查询成功", list);
    }

    ////前端检查项进行删除数据 进行逻辑删除
    //@DeleteMapping("checkgroup/deleteCheckgroupByid/{id}")
    //public Result delete(@PathVariable int id) {
    //    System.out.println("id = " + id);
    //    Boolean delete = checkGroupService.updateCheckGroupById(id);
    //    return new Result(delete,"保存成功");
    //}
    //
    //前端发过来的添加用户请求以及进行用户的修改
    @PostMapping("setmeal/addsetmeal")
    public Result add(@RequestBody SetmealDTO setmealDTO) {
        Boolean aBoolean = setmealService.saveSetmeal(setmealDTO);
        return new Result(aBoolean, "保存成功");
    }

    //分页进行检查组的初始话
    @ApiOperation(value = "套餐查询", notes = "分页套餐查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前分页页码", required = true, paramType = "form", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", required = true, paramType = "form", dataType = "Integer"),
            @ApiImplicitParam(name = "queryString", value = "查询条件", required = false, paramType = "form", dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    @PostMapping("setmeal/findpage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        return new Result(true, "正确", setmealService.findPage(queryPageBean));
    }
}
